const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const uglify = require('uglifyjs-webpack-plugin'); //js压缩插件
const webpack = require("webpack");

module.exports = {
    entry: {
        index: './src/js/index.js',
        login: "./src/js/login.js",
        register:"./src/js/register.js",
        details:"./src/js/details.js",
        goodsList:"./src/js/goodsList.js",
    },
    output: {
        filename: "./js/[name].js",
        path: path.resolve(__dirname, "./build")
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    // { loader: 'style-loader' },
                    MiniCssExtractPlugin.loader,
                    {
                        loader: 'css-loader',
                        options: {
                            importLoaders: 3
                        }
                    },
                    { loader: 'postcss-loader' }
                    
                ]
            },
            {
                test: /\.scss$/i,
                use: [
                    MiniCssExtractPlugin.loader,
                    "css-loader",
                    "sass-loader"]
            },
            {
                test: /\.(html|htm)$/i,
                loader: "html-loader"
            },
            {
                //排除
                test: /\.(ttf|woff|svg|eot)$/i,
                loader: 'file-loader',
                options: {
                    limit: 20 * 1024,
                    name: '[hash:10].[ext]',
                    outputPath: "./iconfont/"
                }
            },
            {   //打包css js中的图片
                test: /\.(jpg|jpeg|png|gif)$/i,
                loader: "url-loader",
                options: {
                    limit: 1024,
                    name: '[hash:10].[ext]',
                    outputPath: "./images/"  //输出到指定目标
                }
            }
        ]
    },
    devServer: {
        port: 8080,
        open: true,
        contentBase: "./build/"
    },
    plugins: [
        new HtmlWebpackPlugin({
            filename: 'index.html',//打包后的文件名
            minify: {//对html文件进行压缩
                removeAttributeQuotes: true, //去掉属性的双引号
                removeComments: true,//去掉注释
                collapseWhitespace: true,//去掉空白
            },
            chunks: ['index'],//每个html只引入对应的js和css
            inject: "body",
            hash: true, //避免缓存js。
            template: './src/index.html' //打包html模版的路径和文件名称
        }),
        new HtmlWebpackPlugin({
            filename: 'login.html',//打包后的文件名
            minify: {//对html文件进行压缩
                removeAttributeQuotes: true, //去掉属性的双引号
                removeComments: true,//去掉注释
                collapseWhitespace: true,//去掉空白
            },
            chunks: ['login'],//每个html只引入对应的js和css
            inject: "body",
            hash: true, //避免缓存js。
            template: './src/login.html' //打包html模版的路径和文件名称
        }),
        new HtmlWebpackPlugin({
            filename: 'register.html',//打包后的文件名
            minify: {//对html文件进行压缩
                removeAttributeQuotes: true, //去掉属性的双引号
                removeComments: true,//去掉注释
                collapseWhitespace: true,//去掉空白
            },
            chunks: ['register'],//每个html只引入对应的js和css
            inject: "body",
            hash: true, //避免缓存js。
            template: './src/register.html' //打包html模版的路径和文件名称
        }),
        new HtmlWebpackPlugin({
            filename: 'details.html',//打包后的文件名
            minify: {//对html文件进行压缩
                removeAttributeQuotes: true, //去掉属性的双引号
                removeComments: true,//去掉注释
                collapseWhitespace: true,//去掉空白
            },
            chunks: ['details'],//每个html只引入对应的js和css
            inject: "body",
            hash: true, //避免缓存js。
            template: './src/details.html' //打包html模版的路径和文件名称
        }),
        new HtmlWebpackPlugin({
            filename: 'goodsList.html',//打包后的文件名
            minify: {//对html文件进行压缩
                removeAttributeQuotes: true, //去掉属性的双引号
                removeComments: true,//去掉注释
                collapseWhitespace: true,//去掉空白
            },
            chunks: ['goodsList'],//每个html只引入对应的js和css
            inject: "body",
            hash: true, //避免缓存js。
            template: './src/goodsList.html' //打包html模版的路径和文件名称
        }),
        new CleanWebpackPlugin({}),
        new MiniCssExtractPlugin({
            filename: '[name].css',
            chunkFilename: '[id].css',
        }),
        new uglify({
            cache: false,//启用文件缓存
            parallel: true,//使用多进程并行运行来提高构建速度
        }),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: 'jquery',
            'window.jQuery': 'jquery'
        }),
    ],
    optimization: {
        splitChunks: {
            chunks: 'async',
            minSize: 30000,
            minChunks: 1,
            maxAsyncRequests: 5,
            maxInitialRequests: 3,
            automaticNameDelimiter: '~',
            name: true,
            cacheGroups: {
                vendors: {
                    test: /[\\/]node_modules[\\/]/,
                    priority: -10
                },
                default: {
                    minChunks: 2,
                    priority: -20,
                    reuseExistingChunk: true
                }
            }
        }
    }

    , mode: "development"
}